<?php
class Admin_CommentController extends Zend_Controller_Action {
	
    public function init() {
        $this->view->commentlzz = 'active';
    }
    public function listAction() {
	    $comments = array();
	    $courseId = Utils_Global::$params['course_id'];
	    $title = Utils_Global::$params['title'];
	    $id = Utils_Global::$params['id'];
	    $page = Utils_Global::$params['page'];
	    $limit = Utils_Global::$params['limit'];
	    if($page < 1) {
	    	$page = 1;
	    }
	    if(!$limit || $limit <= 0) {
	    	$limit = 10;
	    }
    	$model = Admin_Model_Comment::factory();
    	$condition = array('id' => $id, 'course_id' => $courseId, 'title' => $title, 'offset' => ($page - 1) *$limit, 'limit' => $limit);
    	$comments = $model->select($condition);
    	$total = $model->count($condition);
	    
    	$modelCourse = Admin_Model_Course::factory();
    	$courses = $modelCourse->getCourses();
    	
	    $this->view->comments = $comments;
	    $this->view->totalItem = $total;
	    $this->view->page = $page;
	    $this->view->numRowPerPage = $limit;
	    $this->view->currentUrl = $this->view->serverUrl() . $this->view->url(array()) . '?' . http_build_query($condition);
	    $this->view->courses = $courses;
	    $this->view->params = $condition;
	    $this->view->errMessage = Utils_Global::$params['errMessage'];
	}
	
	public function deleteAction() {
	    $id = Utils_Global::$params['id'];
	    $model = Admin_Model_Comment::factory();
	    $result = $model->delete($id);
	    if($result) {
	    	$errMessage = "Xóa thành công";
	    } else {
	    	$errMessage = "Xóa không thành công, vui lòng thử lại!";
	    }
	    $this->_redirect('/admin/comment/list?errMessage=' . $errMessage);
	}
	
	public function editAction() {
	    $id = intval(Utils_Global::$params['id']);
	    $reply = trim(Utils_Global::$params['reply']);
	    if(!$id) {
	        $this->_redirect('/admin/comment/list');
	    }
	    $model = Admin_Model_Comment::factory();
	    $data = array('reply' => $reply, 'last_update' => time());
	    $result = $model->update($id, $data);
	    if($result) {
	        $errMessage = "Trả lời thành công";
	    } else {
	        $errMessage = "Thao tác thất bại, vui lòng thử lại!";
	    }
	    
	    $this->_redirect('/admin/comment/list?errMessage=' . $errMessage);
	}
	
	public function activateAction() {
	    $id = intval(Utils_Global::$params['id']);
	    $value = trim(Utils_Global::$params['active']);
	    $result = array();
	    if(!$id) {
	    	$result['code'] = 0;
	    	$result['message'] = "Thao tác thất bại, dữ liệu không hợp lệ!";
	    	$this->_helper->json($result);
	    }
	    $model = Admin_Model_Comment::factory();
	    $data = array('active' => $value, 'last_update' => time());
	    $returnVal = $model->update($id, $data);
	    if($returnVal) {
	        $result['code'] = 1;
	        $result['message'] = "Thao tác thành công!";
	    } else {
	        $result['code'] = 0;
	        $result['message'] = "Thao tác thất bại, vui lòng thử lại!";
	    }

	    $this->_helper->json($result);
	}
}
